#ifndef XRTCSDK_XRTC_MODULES_RTP_RTCP_PACKET_TRANSPORT_FEEDBACK_H
#define XRTCSDK_XRTC_MODULES_RTP_RTCP_PACKET_TRANSPORT_FEEDBACK_H
#include "xrtc/rtc/modules/rtp_rtcp/rtcp_packet/rtpfb.h"
#include "xrtc/rtc/modules/rtp_rtcp/rtcp_packet/common_header.h"
#include <vector>
namespace xrtc {
	namespace rtcp {

		class TransportFeedback :public Rtpfb {
		public:
			class ReceivePacket {
			public:
				ReceivePacket(uint16_t sequence_number, int64_t time_ticks) :
					sequence_number_(sequence_number), time_ticks_(time_ticks), received_(true){
				}

				ReceivePacket(uint16_t sequence_number) :
					sequence_number_(sequence_number), received_(false) {
				}
			private:
				uint16_t sequence_number_;
				int64_t time_ticks_ = 0;
				bool received_;

			};
			static const uint8_t kFeedbackMessageType = 15;
			bool Parse(const rtcp::CommonHeader& packet);

			size_t BlockLength() const override;

			 bool Create(uint8_t* packet,
				size_t* index,
				size_t max_length,
				PacketReadyCallback callback) const override;
		private:
			class LastChunk {
			public:
				void Decode(uint16_t chunk, size_t max_size);
				void AppendTo(std::vector<uint8_t>* deltas);
				void Clear();
			private:
				static	const size_t kRunLengthCapacity = 0x1fff;
				static	const size_t kOneBitCapacity = 14;
				static	const size_t kTwoBitCapacity = 7;
				static	const size_t kVectorCapacity = kOneBitCapacity;
				static  const uint8_t kLarge = 2;
				void DecodeRunLength(uint16_t chunk,size_t max_size);
				void DecodeOneBit(uint16_t chunk, size_t max_size);
				void DecodeTwoBit(uint16_t chunk, size_t max_size);

			private:

				uint8_t delta_sizes_[kVectorCapacity];
				size_t size_ = 0;
				bool all_same_ = false;
				bool has_large_delta_ = false;

			};
			void Clear();
		private:
			uint16_t base_seq_no_ = 0;
			int32_t base_time_ticks_ = 0;
			uint8_t feedback_seq_ = 0;
			LastChunk last_chunk_;
			std::vector<ReceivePacket> all_packets_;
			std::vector<ReceivePacket>  received_packets_;
			bool include_lost_ = true;
			bool include_timestamps_  = true;
			size_t size_bytes_ = 0;
		};

	}//namespace rtcp
}//namespace xrtc
#endif //XRTCSDK_XRTC_MODULES_RTP_RTCP_PACKET_TRANSPORT_FEEDBACK_H